package cn.kgc.shiro.realm;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @author 课工场
 * @date 2024/12/5
 * @description  测试用户自定义realm是否能用于用户的认证操作
 */
public class TestCustomerRealm {


    public static void main(String[] args) {


        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        securityManager.setRealm(new CustomerRealm());
        SecurityUtils.setSecurityManager(securityManager);
        // 1.获取认证主体
        Subject subject = SecurityUtils.getSubject();
        String username = "root";
        String password = "123456";   //  hello   word     随机盐  salt

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);

        try {
            subject.login(usernamePasswordToken);
            // 认证通过
            System.out.println(subject.isAuthenticated());
        }catch (UnknownAccountException e){
            //  用户名错误
            e.printStackTrace();
        }catch (IncorrectCredentialsException e){
            // 密码错误
            e.printStackTrace();
        }





    }
}



